#include <vector>
#include <iostream>

using namespace std;

class Date
{
public:
    friend struct less_date_ptr;
    friend struct greater_date_ptr;
    friend ostream& operator<<(ostream& _cout, const Date& d);

    Date(int year = 2022, int month = 10, int day = 4)
        :_year(year)
         ,_month(month)
         ,_day(day)
    {}
    
    bool operator<(const Date& d) const 
    {
        return (_year < d._year) || 
            (_year == d._year && _month < d._month) || 
            (_year == d._year && _month == d._month && _day < d._day);
    }

    bool operator>(const Date& d) const 
    {
        return (_year > d._year) || 
            (_year == d._year && _month > d._month) || 
            (_year == d._year && _month == d._month && _day > d._day);
    }

private:
    int _year;
    int _month;
    int _day;
};

namespace qds
{
    //如果是自定义类型,需要自己重载<和>
    template<class T>
    struct less 
    {
        bool operator()(const T& x, const T& y) const 
        {
            return x < y;
        }
    };

    template<>
    struct less<Date*>
    {
        bool operator()(const Date* x, const Date* y) const
        {
            return *x < *y;
        }

    };

    template<class T>
    struct greater 
    {
        bool operator()(const T& x, const T& y) const
        {
            return x > y;
        }
    };

    template<>
    struct greater<Date*>
    {
        bool operator()(const Date* x, const Date* y) const
        {
            return *x > *y;
        }

    };

    //如果是c语言实现大堆和小堆,需要写两次adjust_up和adjust_down
    //而且只是里面的符号变了,其他的没有变
    //c++可以运用仿函数进行控制,建大堆还是建小堆
    template<class T, class Container = vector<T>, class compare = less<T>>
    class priority_queue
    {
private:
        void adjust_up(size_t child)
        {
            compare com;
            size_t parent = (child - 1) / 2;
            while(child > 0)
            {
                //if (_con[child] > _con[parent])
                if (com(_con[parent], _con[child]))
                {
                    swap(_con[child], _con[parent]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else 
                {
                    break;
                }
            }
        }

        void adjust_down(size_t parent)
        {
            compare com;
            size_t child = (parent * 2) + 1;

            while(child < _con.size())
            {
                //if (child + 1 < _con.size() && _con[child] < _con[child + 1])
                if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
                {
                    ++child;
                }

                //if (_con[parent] < _con[child])
                if (com(_con[parent], _con[child]))
                {
                    swap(_con[parent], _con[child]);
                    parent = child;
                    child = (parent * 2) + 1;
                }
                else 
                {
                    break;
                }
            }

        }

public:
        priority_queue()
        {}

        template<class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
            :_con(first, last)
        {
            //对里面的数据进行处理,也就是建堆
            for (int i = (_con.size() - 1 - 1) / 2; i >= 0; --i)
            {
                adjust_down(i);
            }
        }

        bool empty() const 
        {
            return _con.empty();
        }
        
        size_t size() const 
        {
            return _con.size();
        }

        const T& top() const 
        {
            return _con.front();
        }

        void push(const T& val)
        {
            _con.push_back(val);
            adjust_up(_con.size() - 1);
        }

        void pop()
        {
            swap(_con[0], _con[_con.size() -1]);
            _con.pop_back();
            adjust_down(0);
        }

private:
        Container _con;
    };
}
